﻿#include "beep_window.h"
#include "ui_beep_window.h"
#include "main_window.h"
#include <QPen>
#include <QDebug>

extern Main_Window *pmain_window;
Beep_window::Beep_window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Beep_window)
{   
    timer_obj= new QTimer(this);

    ui->setupUi(this);
    ui->Back_menu->installEventFilter(this);
    ui->dida_on->hide();
    ui->didi_on->hide();
    QPixmap pict;
    pict.load(":/picture/main_window/返回菜单.png");
    ui->Back_menu->setScaledContents(true);
    ui->Back_menu->setPixmap(pict);
    connect(this->timer_obj,&QTimer::timeout,[=](){
        if(this->time_flag == 0){
            this->time_flag = 1;
            pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
            pmain_window->Beep_value_File->write("1",1);
            pmain_window->Beep_value_File->close();
        }
        else{
            this->time_flag = 0;
            pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
            pmain_window->Beep_value_File->write("0",1);
            pmain_window->Beep_value_File->close();
        }
    });

    QPalette pex;
    pex.setColor(QPalette::ButtonText,Qt::black);   //文字颜色
    pex.setColor(QPalette::Button ,QColor(189,189,189));       //背景色

    ui->Beep_but->setStyleSheet("QToolButton{background-color: rgb(236,190,134);border:2px groove gray;border-radius:160px;padding:2px 4px;border-style: outset;}"
                                            "QToolButton:hover{background-color:rgb(236,190,134); color: black;}"
                                            "QToolButton:pressed{background-color:rgb(232,176,108);}");

    ui->dida_off->setStyleSheet("QToolButton{background-color: rgb(236,190,134);border:2px groove gray;border-radius:160px;padding:2px 4px;border-style: outset;}"
                                            "QToolButton:hover{background-color:rgb(236,190,134); color: black;}"
                                            "QToolButton:pressed{background-color:rgb(232,176,108);}");
    ui->dida_on->setStyleSheet("QToolButton{background-color: rgb(236,190,134);border:2px groove gray;border-radius:160px;padding:2px 4px;border-style: outset;}"
                                            "QToolButton:hover{background-color:rgb(236,190,134); color: black;}"
                                            "QToolButton:pressed{background-color:rgb(232,176,108);}");
    ui->didi_off->setStyleSheet("QToolButton{background-color: rgb(236,190,134);border:2px groove gray;border-radius:160px;padding:2px 4px;border-style: outset;}"
                                            "QToolButton:hover{background-color:rgb(236,190,134); color: black;}"
                                            "QToolButton:pressed{background-color:rgb(232,176,108);}");
    ui->didi_on->setStyleSheet("QToolButton{background-color: rgb(236,190,134);border:2px groove gray;border-radius:160px;padding:2px 4px;border-style: outset;}"
                                            "QToolButton:hover{background-color:rgb(236,190,134); color: black;}"
                                            "QToolButton:pressed{background-color:rgb(232,176,108);}");




    ui->Beep_but->setPalette(pex);
    ui->Beep_but->setIcon(QIcon(":/picture/beep_window/喇叭.png"));
    ui->Beep_but->setIconSize(QSize(500,500));


}
void Beep_window::paintEvent(QPaintEvent *event){
    QPainter my_painter(this);
    QPen pen(QColor(0,0,0),10);
    pen.setStyle(Qt::DotLine);

    /*背景颜色*/
    my_painter.setBrush(QBrush(QColor(240,209,164)));
    my_painter.drawRect(this->rect());
    my_painter.setPen(pen);
    my_painter.drawLine(QPoint(0,90),QPoint(900,90));

    pen.setStyle(Qt::SolidLine);
    pen.setWidth(3);
    my_painter.setPen(pen);
    QRectF dida_rect(80,200,50,130);
    my_painter.drawRoundedRect(dida_rect, 10, 10);
    my_painter.drawText(93,230,"ON");
    my_painter.drawText(90,310,"OFF");

    QRectF didi_rect(680,200,50,130);
    my_painter.drawRoundedRect(didi_rect, 10, 10);
    my_painter.drawText(693,230,"ON");
    my_painter.drawText(690,310,"OFF");
}
Beep_window::~Beep_window()
{
    delete ui;
}


void Beep_window::on_Beep_but_pressed()
{
    pmain_window->Beep_directon_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_directon_File->write("out",3);
    pmain_window->Beep_directon_File->close();

    pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_value_File->write("1",1);
    pmain_window->Beep_value_File->close();

    this->timer_obj->stop();

    ui->didi_on->hide();
    ui->didi_off->show();
    ui->dida_on->hide();
    ui->dida_off->show();
}

void Beep_window::on_Beep_but_released()
{
 //   pmain_window->Beep_value_File->write("0",1);
    pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_value_File->write("0",1);
    pmain_window->Beep_value_File->close();
}

void Beep_window::on_dida_off_clicked()
{
    ui->dida_on->show();
    ui->dida_off->hide();

    ui->didi_off->show();
    ui->didi_on->hide();

    this->timer_obj->start(500);

    pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_value_File->write("0",1);
    pmain_window->Beep_value_File->close();
}

void Beep_window::on_dida_on_clicked()
{
    ui->dida_off->show();
    ui->dida_on->hide();
    this->timer_obj->stop();
    pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_value_File->write("0",1);
    pmain_window->Beep_value_File->close();
}

void Beep_window::on_didi_off_clicked()
{
    ui->didi_on->show();
    ui->didi_off->hide();

    ui->dida_off->show();
    ui->dida_on->hide();

    timer_obj->stop();

    pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_value_File->write("1",1);
    pmain_window->Beep_value_File->close();
}

void Beep_window::on_didi_on_clicked()
{
    ui->didi_off->show();
    ui->didi_on->hide();

   this->timer_obj->stop();

    pmain_window->Beep_value_File->open(QIODevice::WriteOnly);
    pmain_window->Beep_value_File->write("0",1);
    pmain_window->Beep_value_File->close();
}
bool Beep_window::eventFilter(QObject *obj, QEvent *event)
{
   if (obj == ui->Back_menu){
        if (event->type() == QEvent::MouseButtonPress) //鼠标点击
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换

            if(mouseEvent->button() == Qt::LeftButton){
                this->hide();
                pmain_window->show();
                         return true;
            }
            else return false;
        }
        else return false;
    }
    else
    {
        // pass the event on to the parent class
        return QWidget::eventFilter(obj, event);
    }
}
